package cn.kgc.shiro;

import cn.kgc.shiro.realm.CustomerRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @author 课工场
 * @date 2024/8/21
 * @description   测试自定义realm的使用
 */
public class TestCustomerRealm {

    public static void main(String[] args) {


        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        //  创建一个自定义realm
        CustomerRealm customerRealm = new CustomerRealm();
        //   设置realm的凭证匹配器  如果不设置 密码的比较会直接通过equals执行
        HashedCredentialsMatcher md5 = new HashedCredentialsMatcher("MD5");
        // 设置凭证匹配器hash的次数
        md5.setHashIterations(10);
        customerRealm.setCredentialsMatcher(md5);
        defaultSecurityManager.setRealm(customerRealm);

        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();
        String username = "root";
        String password = "123456";

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        subject.login(usernamePasswordToken);
    }
}
